import { get, put, post, patch, remove } from '@/api/request'

import store from '@/store'
let port = ''
if (store.getters.publicRoute === 'user') {
  if (store.getters.identity === '1') port = '/api/principal'
  if (store.getters.identity === '2') port = '/api/teacher'
  if (store.getters.identity === '3') port = '/api/expert'
}
if (store.getters.publicRoute === 'admin') port = '/api/admin'

export function $butRequest(btn, structuralData = {}, filter = {}) {
  const api = `${port}/${btn.api}`
  console.log('apiapiapiapi', api)
  return new Promise(function(resolve, reject) {
    console.log('点击的按钮携带数据----btn', btn)
    // console.log('点击的传递的----item', structuralData)
    let requestURL = btn.api
    // 设置请求头
    const header = btn.params.header || {}
    // 设置请求接口
    const urlparams = []
    if (btn.params.path) {
      btn.params.path?.forEach((u) => {
        if (typeof u !== 'string') u = u.toString()
        if (u.indexOf('item.') !== -1) {
          const field = u.split('.')
          console.log('field', field)
          field?.forEach((p) => {
            if (structuralData[p]) {
              urlparams.push(structuralData[p])
            }
          })
        } else {
          urlparams.push(u)
        }
      })
      requestURL += '/' + urlparams.join('/')
      console.log('设置请求接口------url----参数', requestURL)
    }

    const queryparams = {}
    if (btn.params.query) {
      const query_key = Object.keys(btn.params.query)
      const query_values = Object.values(btn.params.query)
      query_values?.forEach((q, index) => {
        if (typeof q !== 'string') q = q.toString()
        if (q.indexOf('item.') !== -1) {
          const v = q.split('.')
          v?.forEach((q) => {
            if (structuralData[q]) {
              queryparams[query_key[index]] = structuralData[q]
            }
          })
        } else {
          queryparams[query_key[index]] = q
        }
      })
      console.log('设置请求接口------query---参数', queryparams)
    }
    const formparams = {}
    if (btn.params.form) {
      const form_values = Object.values(btn.params.form)
      form_values?.forEach((f) => {
        formparams[f.key] = structuralData[f.key]
      })
      console.log('设置请求接口------form---参数', formparams)
    }

    const bodyparams = {}
    if (btn.params.body) {
      const body_key = Object.keys(btn.params.body)
      const body_values = Object.values(btn.params.body)
      body_values?.forEach((b, index) => {
        if (typeof b !== 'string') b = b.toString()
        if (b.indexOf('item.') !== -1) {
          const y = b.split('.')
          y?.forEach((o) => {
            if (structuralData[o]) {
              bodyparams[body_key[index]] = structuralData[o]
            }
          })
        } else {
          if (body_key[index] === 'api_info') {
            bodyparams['api'] = api
          } else {
            bodyparams[body_key[index]] = b
          }
          // bodyparams[body_key[index]] = b
        }
      })
      console.log('设置请求接口------body---参数', bodyparams)
    }
    switch (btn.method) {
      case 'GET':
      case 'get':
        get(
          requestURL,
          { ...formparams, ...bodyparams, ...queryparams, ...filter },
          header
        ).then(response => {
          if (response.code === 0) {
            resolve(response.data)
          } else {
            reject(false)
          }
        }).catch((error) => {
          // console.log('error', error)
          reject(error)
        })
        break
      case 'PUT':
      case 'put':
        console.log({ ...formparams, ...bodyparams, ...queryparams })
        put(
          requestURL,
          { ...formparams, ...bodyparams, ...queryparams }
        ).then(response => {
          if (response.code === 0) {
            resolve(response.data)
          } else {
            reject(false)
          }
        }).catch((error) => {
          // console.log('error', error)
          reject(error)
        })
        break
      case 'DELECT':
      case 'delete':
        console.log({ ...formparams, ...bodyparams, ...queryparams })
        remove(
          requestURL,
          { ...formparams, ...bodyparams, ...queryparams }
        ).then(response => {
          if (response.code === 0) {
            resolve(response.data)
          } else {
            reject(false)
          }
        }).catch((error) => {
          // console.log('error', error)
          reject(error)
        })
        break
      case 'POST':
      case 'post':
        post(
          requestURL,
          { ...formparams, ...bodyparams, ...queryparams }
        ).then(response => {
          if (response.code === 0) {
            resolve(response.data)
          } else {
            reject(false)
          }
        }).catch((error) => {
          // console.log('error', error)
          reject(error)
        })
        break
      case 'PATCH':
      case 'patch':
        patch(
          requestURL,
          { ...formparams, ...bodyparams, ...queryparams }
        ).then(response => {
          if (response.code === 0) {
            resolve(response.data)
          } else {
            reject(false)
          }
        }).catch((error) => {
          // console.log('error', error)
          reject(error)
        })
        break
    }
  })
}
